<%--
  Created by IntelliJ IDEA.
  User: carlst6
  Date: 3/9/12
  Time: 3:28 PM
  To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <meta name="layout" content="main"/>
  <title> - Listing Details</title>
</head>
<body>
    <h1>${listing.name?.encodeAsHTML()}</h1>
    <g:if test="${listing.description?.size() > 0}"><div>Description: ${listing.description?.encodeAsHTML()}</div></g:if>
    <div>Seller: <g:link controller="Customer" action="show" id="${listing.seller.id}">${listing.seller?.getNameFromEMail().encodeAsHTML()}</g:link></div>
    <div>Starting Bid:  <g:formatNumber number="${listing?.startingBidPrice}" type="currency" currencyCode="USD" /></div>
    <div>State: <g:if test="${listing.endDate < new Date()}">ENDED</g:if><g:else>OPEN</g:else></div>
    <div>Auction Duration: ${Math.round(listing?.endDate - listing?.startDate)} days</div>
    <div>Auction Started: <g:formatDate date="${listing?.startDate}" type="datetime" style="MEDIUM"/></div>
    <div>Auction Closes:  <g:formatDate date="${listing?.endDate}" type="datetime" style="MEDIUM"/></div>

<h3>Most Recent Bid</h3>
<g:if test="${listing?.bids?.size() == 0}">No Bids</g:if>
<g:else>
    <div>Highest Bid: <g:formatNumber number="${listing?.bids?.sort{-1*it?.amount}[0]?.amount}" type="currency" currencyCode="USD"/></div>
    <div>Bid Date: <g:formatDate date="${listing?.bids?.sort{-1*it?.amount}[0].bidTime}" format="MMM d h:mma" /> </div>
    <g:if test="${listing.winner != null}"><div>Winner: <g:link controller="Customer" action="show" id="${listing.winner.id}">${listing.winner?.getNameFromEMail().encodeAsHTML()}</g:link></div></g:if>
    <g:else><div>Current Leader:
        <g:if test="${listing?.bids?.size() > 0}"><g:link controller="Customer" action="show" id="${listing?.bids?.sort{-1*it?.amount}[0].bidder.id}">${listing?.bids?.sort{-1*it?.amount}[0].bidder.getNameFromEMail().encodeAsHTML()}</g:link></div></g:if>
    </g:else>
</g:else>

<p></p>
<h3>Place New Bid for this Listing</h3>
<g:if test="${listing.endDate < new Date()}">Bids cannot be placed for closed listings</g:if>
<g:else>
    <g:if test="${flash.message}">
        <div class="message" role="status">${flash.message}</div>
    </g:if>
    <g:hasErrors bean="${bidInstance}">
        <ul class="errors" role="alert">
            <g:eachError bean="${bidInstance}" var="error">
                <li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>
            </g:eachError>
        </ul>
    </g:hasErrors>
    <g:form controller="bid" action="save" >
        <fieldset class="form">
            <g:render template="/bid/form" />
        </fieldset>
        <fieldset class="buttons">
            <g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
        </fieldset>
    </g:form>
</g:else>
</body>
</html>